﻿@model Matrix.MaKaYunShang.Model.Dto.McAccountDto
@using Matrix.MaKaYunShang.BLL
@using Matrix.MaKaYunShang.Model.Enum
@{
    ViewBag.Title = "订单管理";
    ViewBag.PathToBtn = "/Orders/List";
    ViewBag.TextBtn = "刷新";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section css{
    <style type="text/css">
        .nav-tabs li { cursor: pointer; }
        .table hr { margin: 5px 0; border: dashed 1px #aaa; }
        #assignModal { top: 30%; }
        #cancelOrRejectedModal { top: 30%; }
    </style>
}
<div class="row divFrameTitle">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-body">
                姓名、电话：<input type="text" class="form-control input-sm" placeholder="客户姓名、电话" style="width: 110px;display: inline-block" id="keyWords" />
                &nbsp;&nbsp;
                单号：<input type="text" class="form-control input-sm" placeholder="订单号" style="width: 110px;display: inline-block" id="orderNo" />
                &nbsp;&nbsp;
                开始时间：<input type="text" class="form-control input-sm form_datetime_order_search" placeholder="开始时间" style="width: 110px;display: inline-block" id="startTime" />
                &nbsp;&nbsp;
                结束时间：<input type="text" class="form-control input-sm form_datetime_order_search" placeholder="结束时间" style="width: 110px;display: inline-block" id="endTime" />
                <input type="hidden" id="hidOrderStatus" value="@ViewBag.OrderStatus" />                &nbsp;&nbsp;
                <a class="btn btn-primary pull-right" style="margin-left: 10px;" id="btnExport">导出</a>
                <a class="btn btn-primary pull-right" id="btnSearch">查询</a>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                订单列表
            </div>
            <div class="panel-body">
                <ul class="nav nav-tabs">
                    <li class="active">
                        <a data-toggle="tab" tag="1">等待派单</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="0">未支付</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="2">正在配送</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="3">已送到</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="4">已完成</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="5">已取消</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="6">已退回</a>
                    </li>
                    <li>
                        <a data-toggle="tab" tag="-1">全部</a>
                    </li>
                </ul>
                <div class="dataTable_wrapper">
                    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                        <thead>
                            <tr>
                                <th>用户收货信息</th>
                                <th>商品信息</th>
                                <th>金额</th>
                                <th>付款方式</th>
                                <th>票券详情</th>
                                <th>订单编号</th>
                                <th>下单时间</th>
                                <th width="210px;">配送信息</th>
                                <th width="270px;">操作</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="assignModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 300px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">订单分派</h4>
            </div>
            <div class="modal-body">
                <select class="form-control" id="Delivery">
                    @{
                        var mcList = McAccountsBLL.Instance.GetModelListWithRoleInfoByShopId(Model.ShopId, (int)CommonEnum.McAccountRole.Delivery);
                        if (mcList != null)
                        {
                            foreach (var item in mcList)
                            {
                                <option value="@item.AccountId">@item.DisplayName</option>
                            }
                        }
                    }
                </select>
            </div>
            <div class="modal-footer">
                <input type="hidden" id="hidOrderId" />
                <button type="button" class="btn btn-primary" id="btnAssign">确认分派</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="cancelOrRejectedModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 400px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="cancelOrRejectedLabel"></h4>
            </div>
            <div class="modal-body">
                <textarea class="form-control" id="sysRemark" style="height: 80px;"></textarea>
            </div>
            <div class="modal-footer">
                <input type="hidden" id="hidSysRemarkOrderId" />
                <input type="hidden" id="hidUrl" />
                <button type="button" class="btn btn-primary" id="btnSaveSysRemark"></button>
            </div>
        </div>
    </div>
</div>
<iframe id="exportOrderFrame" style="display: none"></iframe>
@section javascript{
    <script src="~/Content/admin/dist/js/pagingHelper.js"></script>
    <script type="text/javascript">
        var dataParams = {};
        var serverProcessPath = "/Orders/Listing?orderStatus=@(ViewBag.OrderStatus)";
        var showColumns = [
            { data: "ConsumerInfo" },
            { data: "CommodityInfo" },
            { data: "PayPrice" },
            { data: "PayWay" },
            { data: "CouponResume" },
            { data: "OrderNo" },
            { data: "OrderTime" },
            { data: "DeliveryInfo" },
            { data: "Action" }
        ];
        pagingHelper.Paging(serverProcessPath, dataParams, showColumns, null, false);
        $("#dataTables-example_wrapper div:first").hide();

        $(function () {
            $('.form_datetime_order_search').datetimepicker({
                format: 'yyyy-mm-dd',
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            });

            $("#btnAssign").click(function () {
                $.post("@Url.Action("Assign")", "orderId=" + $("#hidOrderId").val() + "&deliveryId=" + $("#Delivery").val(), function (data) {
                    if (data.Status) {
                        alertify.success(data.Message);
                    } else {
                        alertify.error(data.Message);
                    }
                    $("#assignModal").modal("hide");
                    reloadOrder();
                });
            });

            $("#btnSaveSysRemark").click(function () {
                var url = $("#hidUrl").val();
                var sysRemark = $("#sysRemark").val();
                if (sysRemark == "") {
                    alertify.alert("请填写" + $("#cancelOrRejectedLabel").html());
                    return;
                }
                $.post(url, "orderId=" + $("#hidSysRemarkOrderId").val() + "&sysRemark=" + encodeURIComponent($("#sysRemark").val()), function (data) {
                    if (data.Status) {
                        alertify.success(data.Message);
                    } else {
                        alertify.error(data.Message);
                    }
                    $("#cancelOrRejectedModal").modal("hide");
                    reloadOrder();
                });
            });

            $(".nav-tabs li a").click(function () {
                $("#hidOrderStatus").val($(this).attr("tag"));
                reloadOrder();
            });

            $(".nav-tabs li a[tag='@(ViewBag.OrderStatus)']").parent().addClass("active").siblings().removeClass("active");

            $("#btnSearch").click(function () {
                reloadOrder();
            });

            $("#btnExport").click(function() {
                alertify.okBtn("确定").cancelBtn("取消").confirm("将导出当前搜索条件下的所有订单，确定导出吗（导出时间较长，请耐心等候）？", function() {
                    $.post("@Url.Action("ExportOrders")", "keyWords=" + $("#keyWords").val() + "&orderNo=" + $("#orderNo").val() + "&startTime=" + $("#startTime").val() + "&endTime=" + $("#endTime").val() + "&orderStatus=" + $("#hidOrderStatus").val(), function(data) {
                        if (data.Status) {
                            $("#exportOrderFrame").attr("src", data.Message);
                        } else {
                            alertify.alert(data.Message);
                        }
                    });
                });
            });
        });

        function detail(id) {
            window.location.href = "@Url.Action("Detail")" + "?id=" + id + "&orderStatus=" + $("#hidOrderStatus").val();
        }

        function assign(id) {
            $("#hidOrderId").val(id);
        }

        function arrived(id) {
            alertify.okBtn("确定").cancelBtn("取消").confirm("确认已送达？", function () {
                $.post("@Url.Action("Arrived")", "orderId=" + id, function (data) {
                    if (data.Status) {
                        alertify.success(data.Message);
                        reloadOrder();
                    } else {
                        alertify.error(data.Message);
                    }
                });
            });
        }

        function complete(id) {
            alertify.okBtn("确定").cancelBtn("取消").confirm("确认已完成？", function () {
                $.post("@Url.Action("Complete")", "orderId=" + id, function (data) {
                    if (data.Status) {
                        alertify.success(data.Message);
                        reloadOrder();
                    } else {
                        alertify.error(data.Message);
                    }
                });
            });
        }

        function cancel(id) {
            $("#sysRemark").val("");
            $("#hidUrl").val("@Url.Action("Cancel")");
            $("#hidSysRemarkOrderId").val(id);
            $("#cancelOrRejectedLabel").html("取消原因");
            $("#btnSaveSysRemark").html("确认取消");
        }

        function back(id) {
            $("#sysRemark").val("");
            $("#hidUrl").val("@Url.Action("Back")");
            $("#hidSysRemarkOrderId").val(id);
            $("#cancelOrRejectedLabel").html("退回原因");
            $("#btnSaveSysRemark").html("确认退回");
        }

        function reloadOrder() {
            pagingHelper.reloadWithDiffPath("@Url.Action("Listing")" + "?keyWords=" + $("#keyWords").val() + "&orderNo=" + $("#orderNo").val() + "&startTime=" + $("#startTime").val() + "&endTime=" + $("#endTime").val() + "&orderStatus=" + $("#hidOrderStatus").val());
        }
    </script>
}